/*
 * tasks.cpp
 *
 *  Created on: Mar 4, 2009
 *      Author: shl
 */

#include "tasks.hpp"

#include "log.hpp"

#include <boost/thread/thread.hpp>
//#include <boost/bind.hpp>
//#include <boost/asio.hpp>


using namespace std;
using namespace boost;

namespace fixstoker {


void
tx_task::do_operator_() const {
	LOG_DEBUG("\ttask: " << this->name_ << " started");
	boost::this_thread::sleep(boost::posix_time::seconds(5));
	LOG_DEBUG("\ttask: " << this->name_ << " finished");
}


void
rx_task::do_operator_() const {
	LOG_DEBUG("\ttask: " << this->name_ << " started");
	boost::this_thread::sleep(boost::posix_time::seconds(5));
	LOG_DEBUG("\ttask: " << this->name_ << " finished");
}


bool analyze_task(const abstract_task& task) {
	bool result = true;

	if (abstract_task::FINISHED != task.status()) {
		LOG_ERROR("task: " << task.name() << " did not finish correctly");
		result = false;
	}

	return result;
}


bool
run_all_tasks() {
	barrier syncBarrier(3);

	tx_task txTaskObj(&syncBarrier);
	rx_task rxTaskObj(&syncBarrier);

	thread tx_thread(txTaskObj);
	thread rx_thread(txTaskObj);

	syncBarrier.wait(); // start synchronization
	syncBarrier.wait(); // stop synchronization

	// analyze the run
	bool result = true;
	result &= analyze_task(txTaskObj);
	result &= analyze_task(rxTaskObj);

	return result;
}

}
